package cn.beforedark.yygh.cmn.service;

import cn.beforedark.yygh.cmn.mapper.DictMapper;
import cn.beforedark.yygh.model.cmn.Dict;
import cn.beforedark.yygh.vo.cmn.DictExcelVo;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;

public class DictExcelDtoListener extends AnalysisEventListener<DictExcelVo> {

    private final DictMapper dictMapper;

    public DictExcelDtoListener(DictMapper mapper) {
        this.dictMapper = mapper;
    }

    @Override
    public void invoke(DictExcelVo vo, AnalysisContext analysisContext) {
        Dict dict = new Dict();
        BeanUtils.copyProperties(vo, dict);

        // 设置为未删除
        dict.setIsDeleted(0);

        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("id", vo.getId());
        Integer count = dictMapper.selectCount(wrapper);
        if (count > 0) {
            dictMapper.updateById(dict);
        } else {
            dictMapper.insert(dict);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
